﻿#include "pjzcalc.h"
#include "ui_pjzcalc.h"

PjzCalc::PjzCalc(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PjzCalc)
{
    ui->setupUi(this);
    ui->comboBox->addItem(QStringLiteral("卡球2股皮筋组"));
    ui->comboBox->addItem(QStringLiteral("卡球4股皮筋组"));
    ui->comboBox->addItem(QStringLiteral("传统皮筋组"));
    ui->comboBox->addItem(QStringLiteral("卡球内穿皮筋组"));
    ui->comboBox->addItem(QStringLiteral("传统内穿皮筋组"));
    ui->comboBox->setCurrentIndex(-1);

    if(!load())
        QMessageBox::critical(0,QStringLiteral("致命错误"),QStringLiteral("无法加载配置文件！"),QMessageBox::Ok);
    else
    {
        ui->radius_2k->setText(QString::number(radius_2k));
        ui->radius_4k->setText(QString::number(radius_4k));
        ui->radius_4c->setText(QString::number(radius_4c));
        ui->kq_inradius->setText(QString::number(kq_inradius));
        ui->kq_exradius->setText(QString::number(kq_exradius));
        ui->ct_inradius->setText(QString::number(ct_inradius));
        ui->ct_exradius->setText(QString::number(ct_exradius));
    }
}

PjzCalc::~PjzCalc()
{
    delete ui;
}

bool PjzCalc::load(const QString &fileName)
{
    QFile file(fileName);
    if(file.exists() && file.open(QFile::ReadOnly))
    {

        QJsonDocument doc;
        doc=QJsonDocument::fromJson(file.readAll());
        if(!doc.isEmpty() && doc.isObject())
        {
            QJsonObject obj=doc.object();
            radius_2k=obj.value("radius_2k").toDouble();
            radius_4k=obj.value("radius_4k").toDouble();
            radius_4c=obj.value("radius_4c").toDouble();

            kq_inradius=obj.value("kq_inradius").toDouble();
            kq_exradius=obj.value("kq_exradius").toDouble();
            ct_inradius=obj.value("ct_inradius").toDouble();
            ct_exradius=obj.value("ct_exradius").toDouble();
            return true;
        }
        else
            return false;
    }
    else
        return false;
}

void PjzCalc::on_btn_calc_clicked()
{
    int index=ui->comboBox->currentIndex();
    if(index == -1 || ui->in_num->text().isEmpty())
        return;

    bool ok;
    _num=ui->in_num->text().toInt(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("无效输入！"));
        return;
    }

    switch (index) {
    case 0:
        _result=radius_2k * 2 * _num / 100;
        genSummary(_result);
        break;
    case 1:
        _result=radius_4k * 4 * _num / 100;
        genSummary(_result);
        break;
    case 2:
        _result=radius_4c * 2 * _num / 100;
        genSummary(_result);
        break;
    case 3:
        _inradius=kq_inradius * 2 * _num / 100;
        _exradius=kq_exradius * 2 * _num / 100;
        genSummary(_inradius,_exradius);
        break;
    case 4:
        _inradius=ct_inradius * _num / 100;
        _exradius=ct_exradius * _num / 100;
        genSummary(_inradius,_exradius);
        break;
    }

    ui->lbl_summary->setText(_summary);
    ui->in_num->clear();
}


void PjzCalc::genSummary(double result)
{
    _summary= QString::number(_num) + QStringLiteral("副 ") +
            ui->comboBox->currentText() + QStringLiteral(" 需要：")
            + "\n"
            + QStringLiteral("皮筋：") + QString::number(result) + QStringLiteral(" 米") + "\n"
            + QStringLiteral("皮兜：") + QString::number(_num) + QStringLiteral(" 个");
}

void PjzCalc::genSummary(double inradius, double exradius)
{
    _summary= QString::number(_num) + QStringLiteral("副 ") +
            ui->comboBox->currentText() + QStringLiteral(" 需要：")
            + "\n"
            + QStringLiteral("内芯：") + QString::number(inradius) + QStringLiteral(" 米") + "\n"
            + QStringLiteral("外皮：") + QString::number(exradius) + QStringLiteral(" 米") + "\n"
            + QStringLiteral("皮兜：") + QString::number(_num) + QStringLiteral(" 个");
}


void PjzCalc::on_btn_save_clicked()
{
    bool ok;
    double tmp;

    tmp=ui->radius_2k->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    radius_2k=tmp;

    tmp=ui->radius_4k->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    radius_4k=tmp;

    tmp=ui->radius_4c->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    radius_4c=tmp;

    tmp=ui->kq_inradius->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    kq_inradius=tmp;

    tmp=ui->kq_exradius->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    kq_exradius=tmp;

    tmp=ui->ct_inradius->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    ct_inradius=tmp;

    tmp=ui->ct_exradius->text().toDouble(&ok);
    if(!ok)
    {
        QMessageBox::critical(this,"ERROR",QStringLiteral("存在无效输入！"));
        return;
    }
    ct_exradius=tmp;

    if(save())
        QMessageBox::information(this,"SUCCESS",QStringLiteral("保存成功！"));
    else
        QMessageBox::information(this,"FAILURE",QStringLiteral("保存失败！"));

}

bool PjzCalc::save(const QString &fileName)
{
    QFile file(fileName);
    if(file.open(QFile::WriteOnly))
    {
        QJsonObject obj;
        obj.insert("radius_2k",radius_2k);
        obj.insert("radius_4k",radius_4k);
        obj.insert("radius_4c",radius_4c);
        obj.insert("kq_inradius",kq_inradius);
        obj.insert("kq_exradius",kq_exradius);
        obj.insert("ct_inradius",ct_inradius);
        obj.insert("ct_exradius",ct_exradius);

        QJsonDocument doc(obj);
        file.write(doc.toJson());
        file.close();
        return true;
    }
    else
        return false;
}
